<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * Extract function and arguments from short URLs for this module, and pass
 * them back to xarGetRequestInfo()
 *
 * @author the Example module development team
 * @param  array $params array containing the different elements of the virtual path
 * @return array containing func the function to be called and args the query
 *          string arguments, or empty if it failed
 */
function conversations_userapi_decode_shorturl($params)
{
    /* Initialise the argument list we will return */
    $args = array();
    $module = 'conversations';
    /* Check and see if we have a module alias */
    $aliasisset = xarModGetVar('conversations', 'useModuleAlias');
    $aliasname = xarModGetVar('conversations','aliasname');
    if (($aliasisset) && isset($aliasname)) {
        $usealias   = true;
    } else{
        $usealias = false;
    }

    /* Analyse the different parts of the virtual path
     * $params[1] contains the first part after index.php/example
     * In general, you should be strict in encoding URLs, but as liberal
     * as possible in trying to decode them...
     */
    if ($params[0] != $module) { /* it's possibly some type of alias */
        $aliasname = xarModGetVar('conversations','aliasname');
    }
    if (empty($params[1])) {
        /*( nothing specified -> we'll go to the main function */
        return array('main', $args);
    } elseif (preg_match('/^_new/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('new', $args);
    } elseif (preg_match('/^_trss/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('tag_rss', $args);
    } elseif (preg_match('/^_dt/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('delete_tag', $args);
    } elseif (preg_match('/^_ct/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('change_tag', $args);
    } elseif (preg_match('/^_dp/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('delete_post', $args);
    } elseif (preg_match('/^_rss/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('rss', $args);
    } elseif (preg_match('/^_subscriptions/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('main', array('subs' => 1));
    } elseif (preg_match('/^_read/i', $params[1])) {
        /* something that starts with 'tag' is probably for the tag function
         * Note : make sure your encoding/decoding is consistent ! :-)
         */
        return array('main', array('read' => 1));
    //} elseif (preg_match('/^(\d+)/', $params[1], $matches)) {
        /* something that starts with a number must be for the tag display function
         */
        //$tagid = $matches[1];
        //$args['tagid'] = $tagid;
        //return array('tag', $args);
    } else {
        $args['tagid'] = decodeUsingTitle($params[1]);
        return array('tag', $args);
        
        /* the first part might be something variable like a category name
         * In order to match that, you'll have to retrieve all relevant
         * categories for this module, and compare against them...
         * $cid = xarModGetVar('example','mastercids');
         * if (xarModAPILoad('categories','user')) {
         * $cats = xarModAPIFunc('categories',
         * 'user',
         * 'getcat',
         * array('cid' => $cid,
         * 'return_itself' => true,
         * 'getchildren' => true));
         * // lower-case for fanciful search engines/people
         * $params[1] = strtolower($params[1]);
         * $foundcid = 0;
         * foreach ($cats as $cat) {
         * if ($params[1] == strtolower($cat['name'])) {
         * $foundcid = $cat['cid'];
         * break;
         * }
         * }
         * // check if we found a matching category
         * if (!empty($foundcid)) {
         * $args['cid'] = $foundcid;
         * // TODO: now analyse $params[2] for index, list, \d+ etc.
         * // and return array('whatever', $args);
         * }
         * }
         * we have no idea what this virtual path could be, so we'll just
         * forget about trying to decode this thing
         * you *could* return the main function here if you want to
         * return array('main', $args);
         */
    }
    /* default : return nothing -> no short URL decoded */
}
function decodeUsingTitle($tagname){
  
    $tagname = rawurldecode($tagname);
      
    $tagid = xarModAPIFunc('conversations', 'user', 'gettags', array(
                                                    'tag'   => $tagname));
    
    // Break down the array
    foreach ($tagid as $goofy){
        $id = $goofy['id'];
    }
    
    // heh, more than one way to skin the cat.  Not sure why this snippet won't work in
    // xaruser/tag.php, but it has something to do with the short urls.
    
    // Set the counter up one
    xarModAPIFunc('conversations', 'user', 'updatetag', array(
                                        'tagid'   => $id,
                                        'count'   => 1));
                                        
    // Update our last visit function
    xarModAPIFunc('conversations', 'user', 'updatevisits', array(
                                    'tagid'   => $id,
                                    'uid'   => xarUserGetVar('uid')));
    
    
    return $id;
}
?>